package TestPaperSystem.service.impl;

import TestPaperSystem.entity.ExamConfig;
import TestPaperSystem.mapper.ExamConfigMapper;
import TestPaperSystem.service.ExamConfigService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ExamConfigServiceImpl extends ServiceImpl<ExamConfigMapper, ExamConfig> implements ExamConfigService {

    @Resource
    ExamConfigMapper examConfigMapper;

    @Override
    public Boolean saveData(ExamConfig examConfig) throws Exception{

        if(examConfig.getId() == null){
            return this.save(examConfig);
        }else
            return this.updateById(examConfig);

    }

    @Override
    public ExamConfig getConfigByExamId(String examId) throws Exception{

        List<ExamConfig> examConfigs = examConfigMapper.selectList(new QueryWrapper<ExamConfig>().lambda().eq(ExamConfig::getExamId, examId));
        if(examConfigs.size() > 1)
            throw new Exception("数据有异常");
        return examConfigs.get(0);
    }
}
